package com.cskaoyan.service.promotion_service;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.bean.bo.CouponCreateBO;
import com.cskaoyan.bean.bo.CouponDeleteBO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.bean.vo.promotion_vo.*;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/7 13:57
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public CouponListVO list(BaseParam param, Coupon coupon) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        PageHelper.startPage(page, limit);

        String name = coupon.getName();
        Short type = coupon.getType();
        Short status = coupon.getStatus();

        List<CouponListInfoVO> couponListInfoVOS =  couponMapper.select(name,type,status,order,sort);

        PageInfo<CouponListInfoVO> pageInfo = new PageInfo<CouponListInfoVO>(couponListInfoVOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        AdVO adVo = new AdVO();
        CouponListVO couponListVo = new CouponListVO();
        couponListVo.setTotal(total);
        couponListVo.setPages(pages);
        couponListVo.setLimit(limit);
        couponListVo.setPage(page);
        couponListVo.setList(couponListInfoVOS);
        return couponListVo;
    }

    @Override
    public void deleteCoupon(CouponDeleteBO coupon) {
        couponMapper.deleteByPrimaryKey(coupon.getId());
    }

    @Override
    public CouponCreateVO createCoupon(CouponCreateBO createBO) {
        Coupon coupon = new Coupon();
        //如果是兑换码类型的优惠券，随机生成八位数的兑换码
        if (createBO.getType() == 2){
            Random random = new Random();
            StringBuffer stringBuffer = new StringBuffer();
            String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int length = str.length();
            for (int i = 0; i < 8; i++) {
                int index = random.nextInt(length);
                stringBuffer.append(str.charAt(index));
            }
            String codeDate = stringBuffer.toString();
            coupon.setCode(codeDate);
        }

        coupon.setName(createBO.getName());
        coupon.setDesc(createBO.getDesc());
        coupon.setTag(createBO.getTag());
        coupon.setTotal(createBO.getTotal());
        coupon.setDiscount(createBO.getDiscount());
        coupon.setMin(createBO.getMin());
        coupon.setLimit(createBO.getLimit());
        coupon.setType(createBO.getType());
        coupon.setStatus(createBO.getStatus());
        coupon.setGoodsType(createBO.getGoodsType());
        coupon.setTimeType(createBO.getTimeType());
        coupon.setDays(createBO.getDays());
        coupon.setStartTime(createBO.getStartTime());
        coupon.setDeleted(false);
        if (createBO.getTimeType() != 0) {
            coupon.setEndTime(createBO.getEndTime());
        }
        coupon.setGoodsValue(createBO.getGoodsValue());
        coupon.setAddTime(new Date());

        coupon.setAddTime(new Date());
        //int insertSelective = couponMapper.insert(coupon);
        Integer id = couponMapper.selectMaxId();
        CouponCreateVO createVO = new CouponCreateVO();
        createVO.setId(id);
        createVO.setName(coupon.getName());
        createVO.setDesc(coupon.getDesc());
        createVO.setTag(coupon.getTag());
        createVO.setTotal(coupon.getTotal());
        createVO.setDiscount(coupon.getDiscount());
        createVO.setMin(coupon.getMin());
        createVO.setLimit(coupon.getLimit());
        createVO.setType(coupon.getType());
        createVO.setStatus(coupon.getStatus());
        createVO.setGoodsType(coupon.getGoodsType());
        createVO.setGoodsValue(coupon.getGoodsValue());
        createVO.setTimeType(coupon.getTimeType());
        createVO.setDays(coupon.getDays());
        createVO.setAddTime(coupon.getAddTime());
        createVO.setUpdateTime(coupon.getUpdateTime());
        return createVO;
    }

    @Override
    public Coupon readCoupon(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public CouponListUserVO couponListUser(BaseParam param, Integer couponId, Integer userId, Short status) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        PageHelper.startPage(page, limit);
        CouponUserExample example = new CouponUserExample();
        CouponUserExample.Criteria criteria = example.createCriteria();
        //couponId、userId、status动态sql拼接
        if (couponId != null && !"".equals(couponId)) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null && !"".equals(status)) {
            criteria.andStatusEqualTo(status);
        }
        example.setOrderByClause(sort + " " + order);

        List<CouponUser> couponUserList = couponUserMapper.selectByExample(example);
        PageInfo<CouponUser> pageInfo = new PageInfo(couponUserList);

        CouponListUserVO listUserVO = new CouponListUserVO();
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        listUserVO.setTotal(total);
        listUserVO.setPages(pages);
        listUserVO.setLimit(limit);
        listUserVO.setPage(page);
        listUserVO.setList(couponUserList);
        return listUserVO;
    }

    @Override
    public Coupon updateCoupon(Coupon coupon) {
        coupon.setUpdateTime(new Date());
        couponMapper.updateByPrimaryKeySelective(coupon);
        return coupon;
    }
}
